<template>
  <div class="app-container">
     <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
       <el-form-item label="模糊检索" prop="searchValue">
         <el-input
           v-model="queryParams.searchValue"
           placeholder="设备编码/设备名称/IMEI/ICCID"
           clearable
           style="width: 240px"
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>
       <el-form-item label="安装路段" prop="roadSectionId">
         <el-select v-model="queryParams.roadSectionId" placeholder="请选择" style="width: 100%">
           <el-option
             v-for="item in roadSectionList"
             :key="item.id"
             :label="item.name"
             :value="item.id"
           />
         </el-select>
       </el-form-item>
       <el-form-item label="设备类型" prop="deviceType">
         <el-select v-model="queryParams.deviceType" placeholder="请选择" clearable style="width: 240px">
           <el-option
             v-for="dict in dict.type.device_type"
             :key="dict.value"
             :label="dict.label"
             :value="dict.value"
           />
         </el-select>
       </el-form-item>
       <el-form-item label="设备状态" prop="deviceStatus">
         <el-select v-model="queryParams.deviceStatus" placeholder="请选择" clearable style="width: 240px">
           <el-option
             v-for="dict in dict.type.device_status"
             :key="dict.value"
             :label="dict.label"
             :value="dict.value"
           />
         </el-select>
       </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openSelectDevice"
          v-hasPermi="['system:role:add']"
        >分配设备</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="cancelGroupDeviceAll"
          v-hasPermi="['system:role:remove']"
        >批量移除分组</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-export"
          size="mini"
          @click="handleClose"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="50"/>
      <el-table-column label="设备编码" align="center" prop="deviceCode" width="150" >
        <template slot-scope="scope">
          <a class="link-type" @click="handleDeviceInfo(scope.row)">{{scope.row.deviceCode}}</a>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="deviceName" width="150" >
        <template slot-scope="scope">
          <a class="link-type" @click="handleDeviceInfo(scope.row)">{{scope.row.deviceName}}</a>
        </template>
      </el-table-column>
      <el-table-column label="ICCID" align="center" prop="iccid" width="150" />
      <el-table-column label="IMEI" align="center" prop="imei" width="150" />
      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_type" :value="scope.row.deviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="安装路段" align="center" prop="roadSection" width="100" />
      <el-table-column label="分组名称" align="center" prop="groupName" width="100" />
      <el-table-column label="负责人" align="center" prop="leader" width="100" />
      <el-table-column label="安装位置" align="center" prop="location" width="100" />
      <el-table-column label="分组时间" align="center" prop="installDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.groupTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备状态" align="center" prop="deviceStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_status" :value="scope.row.deviceStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="right" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDeviceInfo(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            style="color:#F56C6C"
            icon="el-icon-circle-close"
            @click="cancelGroupDevice(scope.row)"
          >移除分组</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <selectDevice ref="select" :groupId="queryParams.groupId" @ok="handleQuery" />
    <!--引入设备详情页面-->
    <deviceInfo ref="deviceInfo"></deviceInfo>
  </div>
</template>

<script>
import { getGroupDeviceList, getDeviceManage,groupDeviceCancel,groupDeviceCancelAll} from "@/api/business/device/deviceManage"
import { getDeploymentLocationByClassLevel } from "@/api/business/device/deploymentLocation"
import selectDevice from "./selectDevice"
import deviceInfo from '@/views/business/device/deviceManage/deviceInfo.vue' //引入设备详情页面
export default {
  name: "DeviceList",
  dicts: ['sys_normal_disable',,'device_type','device_status','device_type'],
  components: { selectDevice,deviceInfo },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中用户组
      userIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备分组列表表格数据
      deviceList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: undefined,
        searchValue: undefined,
        deviceType:undefined,
        deviceStatus:undefined
      },
      roadSectionList:[],//路段集合
    }
  },
  created() {
    /*const groupId = this.$route.params && this.$route.params.groupId
    if (groupId) {
      this.queryParams.groupId = groupId
      this.getList()
    }*/

    const groupId =this.$route.query && this.$route.query.groupId
    if (groupId) {
      this.queryParams.groupId = groupId
      this.getList()
    }
    this.getRoadSection()//加载路段
  },
  methods: {
    /** 查询分组设备列表 */
    getList() {
      this.deviceList=[]
      this.loading = true
      getGroupDeviceList(this.queryParams).then(response => {
          this.deviceList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    /** 设备详情 */
    handleDeviceInfo(row) {
      const id = row.id || this.ids
      this.$refs.deviceInfo.deviceInfo(id)
    },
    // 返回按钮
    handleClose() {
      const obj = { path: "/device/deviceGroup" }
      this.$tab.closeOpenPage(obj)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.groupIds = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 打开分组设备表弹窗 */
    openSelectDevice() {
      this.$refs.select.show()
    },
    /** 取消分组设备按钮操作 */
    cancelGroupDevice(row) {
      const groupId = this.queryParams.groupId
      this.$modal.confirm('确认要取消该设备分组吗？').then(function() {
        return groupDeviceCancel({ deviceId: row.id, groupId: groupId })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("取消成功")
      }).catch(() => {})
    },
    /** 批量取消授权按钮操作 */
    cancelGroupDeviceAll(row) {
      const groupId = this.queryParams.groupId
      const deviceIds = this.deviceIds.join(",")
      this.$modal.confirm('是否取消选中设备分组数据项？').then(function() {
        return groupDeviceCancelAll({ groupId: groupId, deviceIds: deviceIds })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("取消成功")
      }).catch(() => {})
    },
    /** 查询路段 */
    getRoadSection() {
      this.roadSectionList=[]
      getDeploymentLocationByClassLevel("0").then(response => {
        this.roadSectionList = response.dataconsole.log( this.roadSectionList)
      })
    },
  }
}
</script>
